<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:lang="${#locale}">
<head>
    <meta charset="utf-8">
    <title th:text="${@portal.name}"></title>
    <meta name="robots" content="NONE">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link th:href="@{/layui/css/layui.css}" rel="stylesheet">
    <style>
        .login-container {
            width: 320px;
            position:fixed;
            top:50%;
            left:50%;
            transform: translate(-50%, -50%);
        }
    </style>
</head>
<body style="background-color: #fafafa">
<form class="layui-form">
    <fieldset class="layui-elem-field login-container">
        <legend th:text="${@portal.name}"></legend>
        <br>
        <div class="layui-field-box">
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input type="text" name="username" value="" lay-verify="required" placeholder="用户名"
                           lay-reqtext="请填写用户名" autocomplete="off" class="layui-input" lay-affix="clear">
                </div>
            </div>
            <br>
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input type="password" name="password" value="" lay-verify="required" placeholder="密   码"
                           lay-reqtext="请填写密码" autocomplete="off" class="layui-input" lay-affix="eye">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-vercode"></i>
                            </div>
                            <input type="text" name="captcha" value="" lay-verify="required" placeholder="验证码" lay-reqtext="请填写验证码" autocomplete="off" class="layui-input" lay-affix="clear">
                        </div>
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <img th:src="@{/api/captcha}" onclick="this.src='/api/captcha?'+ new Date().getTime();">
                        </div>
                    </div>
                </div>
            </div>
            <br>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="login">登录</button>
            </div>
        </div>
    </fieldset>
</form>
<script th:src="@{/layui/layui.js}"></script>
<script>
    layui.use(function () {
        let form = layui.form;
        let layer = layui.layer;
        let $ = layui.jquery;
        // 提交事件
        form.on('submit(login)', function (data) {
            let field = data.field; // 获取表单字段值
            let timestamp = Date.parse(new Date());
            let nonce = Math.floor(Math.random() * (999 - 100 + 1) + 100); // 100-999 random
            let signature = window.btoa(`${timestamp}${nonce}`);
            let payload = window.btoa(JSON.stringify(field));
            $.ajax({
                url: "/api/login",
                type: "POST",
                dataType: "json",
                headers: {
                    "Content-Type": "application/json;charset=utf8",
                    "X-Ca-Timestamp": timestamp,
                    "X-Ca-Nonce": nonce,
                    "X-Ca-Signature": signature,
                    "X-Ca-Signature-Method": "BASE64",
                },
                data: payload,
                success: function () {
                    window.location.href = "/admin";
                },
                error: function (response) {
                    layer.msg(response.responseJSON?.msg);
                },
            })
            return false; // 阻止默认 form 跳转
        });
    });
</script>
</body>
</html>
